<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否置顶" prop="isFrist">
        <el-select v-model="queryParams.isFrist" placeholder="请选择是否置顶" clearable>
          <el-option v-for="dict in dict.type.sys_is_0_1" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="显示状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择显示状态" clearable>
          <el-option v-for="dict in dict.type.sys_is_0_1" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['demo:cover:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['demo:cover:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['demo:cover:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['demo:cover:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="coverList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="首页标题" align="center" prop="subtitleTitle" />
      <el-table-column label="封面" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="是否置顶" align="center" prop="isFrist">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_0_1" :value="scope.row.isFrist" />
        </template>
      </el-table-column>
      <el-table-column label="显示状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_0_1" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['demo:cover:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['demo:cover:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改封面管理抽屉 -->
    <el-drawer :title="title" :visible.sync="open" direction="ltr" size="70%" :before-close="cancel">
      <div class="drawer-content">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-divider content-position="left">基本信息</el-divider>
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入文章标题" />
          </el-form-item>
          <el-form-item label="首页标题" prop="subtitleTitle">
            <el-input v-model="form.subtitleTitle" placeholder="请输入副标题【首页封面使用-不填：只显示图片】" />
          </el-form-item>
          <el-form-item label="封面图" prop="image">
            <image-upload v-model="form.image" />
          </el-form-item>

          <el-divider content-position="left">内容信息</el-divider>



          <el-form-item label="">
            <!-- 图片用base64存储,url方式移动端会显示异常 -->
            <el-row style="margin-bottom: 20px;">
              <el-col align="right">
                <span
                  style="color: #E6A23C;margin-right: 20px;">保存后会覆盖其他富文本编辑器内容</span>
                编辑器：
                <el-select v-model="contentType" placeholder="请选择">
                  <el-option key="1" label="Quill富文本" value="1" />
                  <el-option key="2" label="Markdown" value="2" />
                  <!-- <el-option key="3" label="Tinymce富文本" value="3" /> -->
                </el-select>
              </el-col>
            </el-row>
            <el-row>
              <el-col>
                <editor v-if="contentType === '1'" v-model="form.content" 
                  :min-height="400" />
                <CherryMarkdown ref="CherryMarkdown" v-if="contentType === '2'" :height='400' v-model='form.content'>
                </CherryMarkdown>
                <!-- <Tinymce v-if="contentType === '3'" :height='400' v-model='form.content'></Tinymce> -->
              </el-col>
            </el-row>
          </el-form-item>



          <el-divider content-position="left">媒体资源</el-divider>
          <el-form-item label="图片上传" prop="imgUrl">
            <image-upload v-model="form.imgUrl" />
          </el-form-item>
          <el-form-item label="视频上传" prop="videoUrl">
            <file-upload v-model="form.videoUrl" />
          </el-form-item>

          <el-divider content-position="left">状态设置</el-divider>
          <el-form-item label="是否置顶" prop="isFrist">
            <el-select v-model="form.isFrist" placeholder="请选择是否置顶">
              <el-option v-for="dict in dict.type.sys_is_0_1" :key="dict.value" :label="dict.label"
                :value="dict.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="显示状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio v-for="dict in dict.type.sys_is_0_1" :key="dict.value" :label="dict.value">{{ dict.label
                }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div class="drawer-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { listCover, getCover, delCover, addCover, updateCover } from "@/api/demo/cover";
import CherryMarkdown from '@/components/CherryMarkdown'
// import Tinymce from '@/components/Tinymce'


export default {
  name: "Cover",
  dicts: ['sys_is_0_1'],
  components: {
    CherryMarkdown,
    // Tinymce
  },
  data() {
    return {
      contentType: "1",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 封面管理表格数据
      coverList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        isFrist: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询封面管理列表 */
    getList() {
      this.loading = true;
      listCover(this.queryParams).then(response => {
        this.coverList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        subtitleTitle:null,
        image: null,
        content: null,
        imgUrl: null,
        videoUrl: null,
        isFrist: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加封面管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCover(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改封面管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCover(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCover(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除封面管理编号为"' + ids + '"的数据项？').then(function () {
        return delCover(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('demo/cover/export', {
        ...this.queryParams
      }, `cover_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.drawer-content {
  padding: 20px;
  height: 100%;
  box-sizing: border-box;
}

.drawer-footer {
  position: absolute;
  bottom: 20px;
  right: 20px;
}

.el-divider {
  margin: 30px 0 20px 0;
}

.el-form-item {
  margin-bottom: 20px;
}
</style>